<template>
  <div class="home">
    <div class="wrap">
      <ul class="nav">
        <li
          v-for="(item, index) in fenlei"
          :key="item._id"
          @click="change(index, item._id)"
          :class="{ active: flag == index }"
        >
          {{ item.title }}
        </li>
      </ul>
    </div>
    <!-- 
    <ul class="con">
      <li v-for="item in result" :key="item._id" @click="gotodetail(item)">
        <img :src="item.pic" alt="" />
        <h3>{{ item.title }}</h3>
        <p>价格是{{ item.price }}</p>
      </li>
    </ul> -->

    <ul class="con">
      <li
        v-for="item in list"
        v-show="item.cid == cateid"
        :key="item._id"
        @click="gotodetail(item)"
      >
        <img :src="item.pic" alt="" />
        <h3>{{ item.title }}</h3>
        <p>价格是{{ item.price }}</p>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fenlei: [],
      //导航中分类的数据
      flag: 0,
      list: [],
      //所有的商品数据
      result: [],
      //筛选出来的数据
      cateid: "",
    };
  },
  created() {
    this.getfenlei();
    this.getlist();
  },
  methods: {
    getfenlei() {
      this.$axios.get("fenlei.json").then((res) => {
        console.log(res);
        this.fenlei = res.data.result;
      });
    },
    //获取分类数据
    getlist() {
      this.$axios.get("list.json").then((res) => {
        console.log(res);
        this.list = res.data.result;
        this.change(0, "59f1e1ada1da8b15d42234e9");
      });
    },
    //获取列表数据
    change(index, id) {
      this.flag = index;
      this.cateid = id;
      // this.result = this.list.filter((item) => {
      //   return item.cid == id;
      // });
      //filter数组的方法 对数组进行筛选 如果满足cid是当前分类的数据 就会被筛选出来防在一个新数组中
    },
    //根据tab对数据进行筛选

    gotodetail(item) {
      this.$router.push({ path: "/detail", query: { item: item } });
    },
  },
};
</script>
<style lang="scss" scoped>
.wrap {
  width: 100%;
  overflow: scroll;
  .nav {
    display: flex;
    li {
      width: 80px;
      // border: 1px solid #ccc;
      flex-shrink: 0;
      //不缩小
      height: 40px;
      line-height: 40px;
    }
  }
}
.active {
  color: brown;
  border-bottom: 2px solid red !important;
}
.con {
  display: flex;
  flex-wrap: wrap;
  // justify-content: space-around;
  li {
    width: 45%;
    margin: 30px 10px;
    img {
      width: 100%;
    }
  }
}
</style>
